const gulp      = require("gulp"),
  sass          = require("gulp-sass"),
  babel         = require("gulp-babel"),
  autoprefixer  = require("gulp-autoprefixer"),
  minifycss     = require("gulp-minify-css"),
  uglify        = require("gulp-uglify"),
  rename        = require("gulp-rename"),
  notify        = require("gulp-notify"),
  sourcemaps    = require("gulp-sourcemaps"),
  watch         = require("gulp-watch"),
  webserver     = require("gulp-webserver");
let config      = require('./config');

const CSSPREFIXER = [
  "last 4 version",
  "safari >= 5",
  "ie > 8",
  "opera >= 12.1",
  "ios >= 9",
  "android >= 4"
]

// ———————— Scripts ————————

gulp.task("scripts", function() {
  return gulp
    .src(config.lib.js)
    .pipe(
      rename({
        suffix: ".min"
      })
    )
    .pipe(
      babel({
        presets: ["es2015"]
      })
    )
    .pipe(uglify())
    .pipe(gulp.dest(config.build.js))
    .pipe(
      notify({
        message: "Scripts task complete"
      })
    );
});

gulp.task("devScripts", function() {
  return gulp
    .src(config.lib.js)
    .pipe(watch(config.lib.js))
    .pipe(sourcemaps.init())
    .pipe(
      rename({
        suffix: ".min"
      })
    )
    .pipe(
      babel({
        presets: ["es2015"]
      })
    )
    .pipe(sourcemaps.write())
    .pipe(gulp.dest(config.build.js))
    .pipe(
      notify({
        message: "Scripts task complete"
      })
    );
});

// ———————— Styles ————————

gulp.task("styles", function() {
  return gulp
    .src(config.lib.css)
    .pipe(
      autoprefixer(CSSPREFIXER)
    )
    .pipe(
      rename({
        suffix: ".min"
      })
    )
    .pipe(minifycss())
    .pipe(gulp.dest(config.build.css))
    .pipe(
      notify({
        message: "Styles task complete"
      })
    );
});
gulp.task("devStyles", function() {
  return gulp
    .src(config.lib.css)
    .pipe(watch(config.lib.css))
    .pipe(sourcemaps.init())
    .pipe(
      autoprefixer(CSSPREFIXER)
    )
    .pipe(
      rename({
        suffix: ".min"
      })
    )
    .pipe(sourcemaps.write())
    .pipe(gulp.dest(config.build.css))
    .pipe(
      notify({
        message: "Styles task complete"
      })
    );
});

gulp.task("sass", function() {
  return gulp
    .src(config.lib.scss)
    .pipe(sass())
    .pipe(
      autoprefixer(CSSPREFIXER)
    )
    .pipe(
      rename({
        suffix: ".min"
      })
    )
    .pipe(minifycss())
    .pipe(gulp.dest(config.build.css))
    .pipe(
      notify({
        message: "Sass task complete"
      })
    );
});
gulp.task("devSass", function() {
  return gulp
    .src(config.lib.scss)
    .pipe(watch(config.lib.scss))
    .pipe(sourcemaps.init())
    .pipe(sass())
    .pipe(
      autoprefixer(CSSPREFIXER)
    )
    .pipe(
      rename({
        suffix: ".min"
      })
    )
    .pipe(sourcemaps.write())
    .pipe(gulp.dest(config.build.css))
    .pipe(
      notify({
        message: "Sass task complete"
      })
    );
});

gulp.task('webserver', function() {
  gulp.src( config.rootPath ) // 服务器目录（代表根目录）
  .pipe(webserver({
    host: config.host,
    livereload: false,
    open: true,
    port: config.port
  }));
});

// , ['cleanjs', 'cleancss']
gulp.task("default", function() {
  gulp.start("sass", "styles", "scripts");
});

gulp.task("watch", function() {
  gulp.start("devSass", "devStyles", "devScripts", "webserver");
});
